package com.example.learnspringboot06datajpa.controller;

import com.example.learnspringboot06datajpa.entity.User;
import com.example.learnspringboot06datajpa.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
@RequestMapping("/user")
public class UersController {
    @Autowired
    UserRepository userRepository;

//    @GetMapping("/user/{id}")//http://localhost:8081/user/2
    @GetMapping("/{id}")//http://localhost:8081/user/2
    public User getUser(@PathVariable("id") Integer id){
        Optional<User> user = userRepository.findById(id);
        if (user == null || user.get() == null){

            return null;
        }
        return user.get();
    }

//    @GetMapping("/user")//http://localhost:8081/user?lastName=test&email=test01@test.com
    @GetMapping//http://localhost:8081/user?lastName=test&email=test01@test.com
    public User insertUser(User user){
        User save = userRepository.save(user);
        return save;
    }
}
